package com.lang.daoandimpl;

import java.util.Date;
import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.lang.beans.Parcel;
import com.lang.beans.Task;
import com.lang.util.Config;

/**
 * 
 * @see com.lang.daoandimpl.Task
 * @author lang
 */

public class TaskDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory.getLogger(TaskDAO.class);

	// property constants

	protected void initDao() {
		// do nothing
	}

	/**
	 * 保存一个任务到数据库
	 * 
	 * @param transientInstance
	 */
	public void save(Task transientInstance) {
		log.debug("saving Task instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/**
	 * 删除一个任务
	 * 
	 * @param persistentInstance
	 */
	public void delete(Task persistentInstance) {
		log.debug("deleting Task instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/**
	 * 通过id获取任务
	 * 
	 * @param id
	 * @return
	 */
	public Task findById(java.lang.Integer id) {
		log.debug("getting Task instance with id: " + id);
		try {
			Task instance = (Task) getHibernateTemplate().get(
					"com.lang.beans.Task", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/**
	 * 通过实例获取任务
	 * 
	 * @param instance
	 * @return
	 */
	public List findByExample(Task instance) {
		log.debug("finding Task instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/**
	 * 通过某个属性获取任务
	 * 
	 * @param propertyName
	 * @param value
	 * @return
	 */
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Task instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Task as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findAll() {
		log.debug("finding all Task instances");
		try {
			String queryString = "from Task";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Task merge(Task detachedInstance) {
		log.debug("merging Task instance");
		try {
			Task result = (Task) getHibernateTemplate().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/**
	 * 通过实例添加或者更新任务
	 * 
	 * @param instance
	 */
	public void attachDirty(Task instance) {
		log.debug("attaching dirty Task instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/**
	 * 通过邮差id获取邮差未登记的快递
	 * 
	 * @param id
	 *            邮差id号
	 * @param status
	 *            需要处理的类型 1：正在处理，2已处理，3异常
	 * @param type
	 *            收件还是派件
	 */
	public List getTasksByCourier(int id, int status, int type) {
		String queryString = null;
		try {
			switch (status) {
			case 1:
				queryString = "from Task as t where t.courier.id = ? and t.parcel.status=1";
				break;
			case 2:
				queryString = "from Task as t where t.courier.id = ? and t.parcel.status=2";
				break;
			case 3:
				queryString = "from Task as t where t.courier.id = ? and t.parcel.status=3";
				break;
			default:
				queryString = "from Task where t.courier.id = ?";
			}
			if (type == 0)
				queryString += " and t.parcel.type=0";
			else
				queryString += " and t.parcel.type=1";
			return getHibernateTemplate().find(queryString, id);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/**
	 * 更新成功派件
	 * 
	 * @param id
	 * @author Lang
	 */
	public Task updateFinishTime(int id, String systime) {
		Task t = null;
		try {
			t = getHibernateTemplate().get(Task.class, id);
			if (t != null) {
				t.setFinish_time(new Date());
				getHibernateTemplate().update(t);
			}
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
		return t;
	}

	public void attachClean(Task instance) {
		log.debug("attaching clean Task instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static TaskDAO getFromApplicationContext(ApplicationContext ctx) {
		return (TaskDAO) ctx.getBean("TaskDAO");
	}
}